package cn.zyl.common.redis.beans;

import cn.zyl.common.redis.constant.RedisConfigConstant;
import cn.zyl.demo.common.boot.constant.CommonBootConstant;
import cn.zyl.demo.common.tools.ObjectTool;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * JedisConfigBean类说明: redis客户端 配置信息 实体
 *
 * @author wsz
 * @version v1.0
 * @date 2020-08-05
 */
public class JedisConfigBean  implements Serializable {
    private static final long serialVersionUID = 1L;
    /** ip*/
    private String host;
    /** 端口*/
    private Integer port;
    /** 密码*/
    private String password ;
    /** 连接超时*/
    private Integer timeout;
    /** 读写超时*/
    private Integer soTimeout;
    /** 数据库索引*/
    private Integer database;
    /** 连接池最大连接数（负值无限制）*/
    private Integer poolMaxActive;
    /** 连接池最大空闲连接数*/
    private Integer poolMaxIdle;
    /** 连接池最小空闲连接数*/
    private Integer poolMinIdle;
    /** 连接重试次数*/
    private Integer maxAttempts;
    /** 连接池最大阻塞等待时间（负值无限制）*/
    private Long poolMaxWait;
    /** 连接耗尽时是否阻塞, false报异常,true阻塞直到超时*/
    private Boolean blockWhenExhausted;
    /** 是否启用pool的jmx管理功能*/
    private Boolean jmxEnabled;
    /** 自定义配置，是否启用连接池*/
    private Boolean poolEnabled;
    /** 自定义配置，是否启用集群*/
    private Boolean clusterEnabled;
    /** 集群配置连接信息*/
    private String clusterNodes;

    public void init(){
        this.host = ObjectTool.isNullByDef(this.host,RedisConfigConstant.DEFAULT_HOST);
        this.port = ObjectTool.isNullByDef(this.port,RedisConfigConstant.DEFAULT_PORT);
        this.password = ObjectTool.isNullByDef(this.password,RedisConfigConstant.DEFAULT_PASSWORD);
        this.timeout = ObjectTool.isNullByDef(this.timeout,RedisConfigConstant.DEFAULT_TIMEOUT);
        this.soTimeout = ObjectTool.isNullByDef(this.soTimeout,RedisConfigConstant.DEFAULT_SO_TIMEOUT);
        this.database = ObjectTool.isNullByDef(this.database,RedisConfigConstant.DEFAULT_DATABASE);
        this.poolMaxActive = ObjectTool.isNullByDef(this.poolMaxActive,RedisConfigConstant.DEFAULT_POOL_MAX_ACTIVE);
        this.poolMaxIdle = ObjectTool.isNullByDef(this.poolMaxIdle,RedisConfigConstant.DEFAULT_POOL_MAX_IDLE);
        this.poolMinIdle = ObjectTool.isNullByDef(this.poolMinIdle,RedisConfigConstant.DEFAULT_POOL_MIN_IDLE);
        this.poolMaxWait = ObjectTool.isNullByDef(this.poolMaxWait,RedisConfigConstant.DEFAULT_POOL_MAX_WAIT);
        this.maxAttempts = ObjectTool.isNullByDef(this.maxAttempts,RedisConfigConstant.DEFAULT_MAX_ATTEMPTS);
        this.blockWhenExhausted = ObjectTool.isNullByDef(this.blockWhenExhausted,RedisConfigConstant.DEFAULT_BLOCK_WHEN_EXHAUSTED);
        this.jmxEnabled = ObjectTool.isNullByDef(this.jmxEnabled,RedisConfigConstant.DEFAULT_JMX_ENABLED);
        this.poolEnabled = ObjectTool.isNullByDef(this.poolEnabled,RedisConfigConstant.POOL_ENABLE);
        this.clusterEnabled = ObjectTool.isNullByDef(this.clusterEnabled,RedisConfigConstant.CLUSTER_ENABLE);
        this.clusterNodes = ObjectTool.isNullByDef(this.clusterNodes,RedisConfigConstant.DEFAULT_CLUSTER_NODES);
    }
    @Override
    public String toString() {
        return "JedisConfigBean{" +
                "host='" + host + '\'' +
                ", port=" + port +
                ", password='" + password + '\'' +
                ", timeout=" + timeout +
                ", soTimeout=" + soTimeout +
                ", database=" + database +
                ", poolMaxActive=" + poolMaxActive +
                ", poolMaxIdle=" + poolMaxIdle +
                ", poolMinIdle=" + poolMinIdle +
                ", maxAttempts=" + maxAttempts +
                ", poolMaxWait=" + poolMaxWait +
                ", blockWhenExhausted=" + blockWhenExhausted +
                ", jmxEnabled=" + jmxEnabled +
                ", poolEnabled=" + poolEnabled +
                ", clusterEnabled=" + clusterEnabled +
                ", clusterNodes='" + clusterNodes + '\'' +
                '}';
    }

    /**
     * 返回实体类的字段说明
     * @return 说明
     */
    public Map<String, Object> toRemarks(){
        Map<String, Object> res = new HashMap<>(CommonBootConstant.NUM_20);
        res.put("host", "redis-单机-IP地址-默认localhost");
        res.put("port", "redis-单机-端口-默认6379");
        res.put("password", "redis-密码-默认空");
        res.put("timeout", "连接超时时间-默认2000");
        res.put("soTimeout", "读写超时时间-默认2000");
        res.put("database", "数据库索引-默认0");
        res.put("poolMaxActive", "连接池-最大连接数（负值无限制）默认8");
        res.put("poolMaxIdle", "连接池-最大空闲连接数-默认8");
        res.put("poolMinIdle", "连接池-最小空闲连接数-默认0");
        res.put("maxAttempts", "连接重试次数-默认5");
        res.put("poolMaxWait", "连接池-最大阻塞等待时间（负值无限制）默认-1L");
        res.put("blockWhenExhausted", "连接耗尽时是否阻塞, false报异常,true阻塞直到超时, 默认true");
        res.put("jmxEnabled", "是否启用pool的jmx管理功能,-默认true");
        res.put("poolEnabled", "自定义配置，是否启用连接池-默认false不启用");
        res.put("clusterEnabled", "自定义配置，是否启用集群-默认false不启用");
        res.put("clusterNodes", "IP和端口的集合字符串-集群版-默认null,例如：ip:port,ip:port,...");
        return res;
    }
    /**
     * 返回实体类的字段说明
     * @return 说明
     */
    public Map<String, Object> toDefVal(){
        Map<String, Object> res = new HashMap<>(CommonBootConstant.NUM_20);
        res.put("host", host);
        res.put("port", port);
        res.put("password", password);
        res.put("timeout", timeout);
        res.put("soTimeout", soTimeout);
        res.put("database", database);
        res.put("poolMaxActive", poolMaxActive);
        res.put("poolMaxIdle", poolMaxIdle);
        res.put("poolMinIdle", poolMinIdle);
        res.put("maxAttempts", maxAttempts);
        res.put("poolMaxWait", poolMaxWait);
        res.put("blockWhenExhausted", blockWhenExhausted);
        res.put("jmxEnabled", jmxEnabled);
        res.put("poolEnabled", poolEnabled);
        res.put("clusterEnabled", clusterEnabled);
        res.put("clusterNodes", clusterNodes);
        return res;
    }
    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getTimeout() {
        return timeout;
    }

    public void setTimeout(Integer timeout) {
        this.timeout = timeout;
    }

    public Integer getSoTimeout() {
        return soTimeout;
    }

    public void setSoTimeout(Integer soTimeout) {
        this.soTimeout = soTimeout;
    }

    public Integer getDatabase() {
        return database;
    }

    public void setDatabase(Integer database) {
        this.database = database;
    }

    public Integer getPoolMaxActive() {
        return poolMaxActive;
    }

    public void setPoolMaxActive(Integer poolMaxActive) {
        this.poolMaxActive = poolMaxActive;
    }

    public Integer getPoolMaxIdle() {
        return poolMaxIdle;
    }

    public void setPoolMaxIdle(Integer poolMaxIdle) {
        this.poolMaxIdle = poolMaxIdle;
    }

    public Integer getPoolMinIdle() {
        return poolMinIdle;
    }

    public void setPoolMinIdle(Integer poolMinIdle) {
        this.poolMinIdle = poolMinIdle;
    }

    public Integer getMaxAttempts() {
        return maxAttempts;
    }

    public void setMaxAttempts(Integer maxAttempts) {
        this.maxAttempts = maxAttempts;
    }

    public Long getPoolMaxWait() {
        return poolMaxWait;
    }

    public void setPoolMaxWait(Long poolMaxWait) {
        this.poolMaxWait = poolMaxWait;
    }

    public Boolean getBlockWhenExhausted() {
        return blockWhenExhausted;
    }

    public void setBlockWhenExhausted(Boolean blockWhenExhausted) {
        this.blockWhenExhausted = blockWhenExhausted;
    }

    public Boolean getJmxEnabled() {
        return jmxEnabled;
    }

    public void setJmxEnabled(Boolean jmxEnabled) {
        this.jmxEnabled = jmxEnabled;
    }

    public Boolean getPoolEnabled() {
        return poolEnabled;
    }

    public void setPoolEnabled(Boolean poolEnabled) {
        this.poolEnabled = poolEnabled;
    }

    public Boolean getClusterEnabled() {
        return clusterEnabled;
    }

    public void setClusterEnabled(Boolean clusterEnabled) {
        this.clusterEnabled = clusterEnabled;
    }

    public String getClusterNodes() {
        return clusterNodes;
    }

    public void setClusterNodes(String clusterNodes) {
        this.clusterNodes = clusterNodes;
    }
}
